//
//  Tools.m
//  one·一个  tyw
//
//  Created by tyw on 15/8/27.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "Tools.h"
#import "AFNetworking.h"

@implementation Tools

+ (NSString *)exchangeMonth:(NSString*)monthNum {
    if ([monthNum isEqualToString:@"01"]) {
        return @"Jan";
    } else if ([monthNum isEqualToString:@"02"]) {
        return @"Feb";
    } else if ([monthNum isEqualToString:@"03"]) {
        return @"Mar";
    } else if ([monthNum isEqualToString:@"04"]) {
        return @"Apr";
    } else if ([monthNum isEqualToString:@"05"]) {
        return @"May";
    } else if ([monthNum isEqualToString:@"06"]) {
        return @"Jun";
    } else if ([monthNum isEqualToString:@"07"]) {
        return @"Jul";
    } else if ([monthNum isEqualToString:@"08"]) {
        return @"Aug";
    } else if ([monthNum isEqualToString:@"09"]) {
        return @"Sep";
    } else if ([monthNum isEqualToString:@"10"]) {
        return @"Oct";
    } else if ([monthNum isEqualToString:@"11"]) {
        return @"Nov";
    } else if ([monthNum isEqualToString:@"12"]) {
        return @"Dec";
    } else {
        NSLog(@"月份序号错误，shabby");
        return @"Jan";
    }
}

//把   2015-09-09  日期的样式转换成   September 9,2015   的样式
+ (NSString*)exchangeDate:(NSString*)dateStr {
    NSArray* array = [dateStr componentsSeparatedByString:@"-"];
    if (array.count == 3) {
        NSString* monthNum = array[1];
        if ([monthNum isEqualToString:@"01"]) {
            return [NSString stringWithFormat:@"January %@,%@", array[2], array[0]];
        } else if ([monthNum isEqualToString:@"02"]) {
            return [NSString stringWithFormat:@"February %@,%@", array[2], array[0]];
        } else if ([monthNum isEqualToString:@"03"]) {
            return [NSString stringWithFormat:@"March %@,%@", array[2], array[0]];
        } else if ([monthNum isEqualToString:@"04"]) {
            return [NSString stringWithFormat:@"April %@,%@", array[2], array[0]];
        } else if ([monthNum isEqualToString:@"05"]) {
            return [NSString stringWithFormat:@"May %@,%@", array[2], array[0]];
        } else if ([monthNum isEqualToString:@"06"]) {
            return [NSString stringWithFormat:@"June %@,%@", array[2], array[0]];
        } else if ([monthNum isEqualToString:@"07"]) {
            return [NSString stringWithFormat:@"July %@,%@", array[2], array[0]];
        } else if ([monthNum isEqualToString:@"08"]) {
            return [NSString stringWithFormat:@"August %@,%@", array[2], array[0]];
        } else if ([monthNum isEqualToString:@"09"]) {
            return [NSString stringWithFormat:@"September %@,%@", array[2], array[0]];
        } else if ([monthNum isEqualToString:@"10"]) {
            return [NSString stringWithFormat:@"October %@,%@", array[2], array[0]];
        } else if ([monthNum isEqualToString:@"11"]) {
            return [NSString stringWithFormat:@"November %@,%@", array[2], array[0]];
        } else if ([monthNum isEqualToString:@"12"]) {
            return [NSString stringWithFormat:@"December %@,%@", array[2], array[0]];
        } else {
            NSLog(@"月份序号错误，shabby");
            return [NSString stringWithFormat:@"January %@,%@", array[2], array[0]];
        }
    } else {
        NSLog(@"日期格式不正确");
        return nil;
    }
}


//根据 最大宽度、字符串内容 计算自适应的尺寸
+ (CGSize)contentSizeWithMaxWidth:(CGFloat)maxWidth text:(NSString*)text fontOfSize:(CGFloat)fontOfSize {
    //maxLabelWidth ： 消息label最大宽度，就是你想让字符串自适应的宽度
    
    //字典，可能存储了这种字体的相关属性，可打印看看
    NSDictionary* dic = @{NSFontAttributeName:[UIFont systemFontOfSize:fontOfSize]};
    //自适应后获得这个label的size
    CGSize contentSize = [text boundingRectWithSize:CGSizeMake(maxWidth, 50000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
    return contentSize;
}

//把数组或字典转化成json数据
+ (NSString *)getJsonStrFromObj:(id)obj {
    NSError* error = nil;
    NSData* data = [NSJSONSerialization dataWithJSONObject:obj options:NSJSONWritingPrettyPrinted error:&error];
    if (error) {
        NSLog(@"转换成json数据失败  error%@", error);
        return nil;
    }
    NSString* jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return jsonStr;
}




+ (void)saveMessage:(NSString *)messType strCt:(NSNumber *)strCt date:(NSString *)date {
    if (![messType isEqualToString:@"like"] && ![messType isEqualToString:@"praise"]) {
        NSLog(@"收藏还是点赞啊哥，说清楚");
        return;
    }
    
    //存储到本地的信息，是一个数组，它的键值是@"likePraiseMessageArray"
    //数组的每一个元素都是一个字典
    //每一个字典有3个元素，它们的键值分别是messType、strCt、date
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray* likePraiseMessageArray = [defaults objectForKey:@"likePraiseMessageArray"];
    if (likePraiseMessageArray == nil) {
        likePraiseMessageArray = [[NSMutableArray alloc] init];
    }
    
    NSDictionary* dic = @{@"messType" : messType,
                          @"strCt" : strCt,
                          @"date" : date
                          };
    
    //需要一个中间数组来承接，否则段错误。暂时不懂为什么
    NSMutableArray* array = [NSMutableArray arrayWithArray:likePraiseMessageArray];
    likePraiseMessageArray = nil;
    
    [array addObject:dic];
    
    [defaults setObject:array forKey:@"likePraiseMessageArray"];
    [defaults synchronize];
}

+ (void)deleteMessage:(NSString *)messType strCt:(NSNumber *)strCt date:(NSString *)date {
    if (![messType isEqualToString:@"like"] && ![messType isEqualToString:@"praise"]) {
        NSLog(@"收藏还是点赞啊哥，说清楚");
        return;
    }
    
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray* likePraiseMessageArray = [defaults objectForKey:@"likePraiseMessageArray"];
    if (likePraiseMessageArray == nil) {
        return;
    }
    
    NSMutableArray* array = [NSMutableArray arrayWithArray:likePraiseMessageArray];
    likePraiseMessageArray = nil;
    
    //遍历数组，把需要删除的字典删除
    for (NSDictionary* dic in likePraiseMessageArray) {
        NSString* savedMessType = dic[@"messType"];
        NSNumber* savedStrCt = dic[@"strCt"];
        NSString* savedDate = dic[@"date"];
        
        //如果三个元素都一样，就删除。跳出循环
        if ([savedMessType isEqualToString:messType] && [savedStrCt integerValue] == [strCt integerValue] && [savedDate isEqualToString:date]) {
            [array removeObject:dic];
            break;
        }
    }
    
    //再把最新的数据存储到本地
    [defaults setObject:array forKey:@"likePraiseMessageArray"];
    [defaults synchronize];
}


//查看相关页面是否已收藏
+ (BOOL)likedStrCt:(NSNumber *)strCt date:(NSString *)date {
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray* likePraiseMessageArray = [defaults objectForKey:@"likePraiseMessageArray"];
    if (likePraiseMessageArray == nil) {
        return NO;
    }
    
    //遍历数组，把需要删除的字典删除
    for (NSDictionary* dic in likePraiseMessageArray) {
        NSString* savedMessType = dic[@"messType"];
        NSNumber* savedStrCt = dic[@"strCt"];
        NSString* savedDate = dic[@"date"];
        
        //如果三个元素都一样，就删除。跳出循环
        if ([savedMessType isEqualToString:@"like"] && [savedStrCt integerValue] == [strCt integerValue] && [savedDate isEqualToString:date]) {
            return YES;
        }
    }
    
    return NO;
}

//查看相关页面是否已点赞
+ (BOOL)praisedStrCt:(NSNumber *)strCt date:(NSString *)date {
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray* likePraiseMessageArray = [defaults objectForKey:@"likePraiseMessageArray"];
    if (likePraiseMessageArray == nil) {
        return NO;
    }
    
    //遍历数组，把需要删除的字典删除
    for (NSDictionary* dic in likePraiseMessageArray) {
        NSString* savedMessType = dic[@"messType"];
        NSNumber* savedStrCt = dic[@"strCt"];
        NSString* savedDate = dic[@"date"];
        
        //如果三个元素都一样，就删除。跳出循环
        if ([savedMessType isEqualToString:@"praise"] && [savedStrCt integerValue] == [strCt integerValue] && [savedDate isEqualToString:date]) {
            return YES;
        }
    }
    
    return NO;
}


@end
